#include "stm32f10x.h"                  // Device header

//测周法适于低频
//此方法所测频率不能低于15Hz否则时钟计数器将溢出
//频率大于10kHz时误差大于1% 
void InputCapture_Init(void) 
{
	//启用GPIOA0
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM3使能
	TIM_InternalClockConfig(TIM3); //选择内部时钟作为时钟源，可省略生
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 65536-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
	
	TIM_ICInitTypeDef TIM_ICInitStructure; //配置输入捕获单元
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;  //TIM3CH1为PA6
	TIM_ICInitStructure.TIM_ICFilter = 0xf; //滤波器
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //不分频
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //直连通道
	TIM_ICInit(TIM3,&TIM_ICInitStructure);
	
	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
	TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
	
	TIM_Cmd(TIM3,ENABLE); //启动计时器
}

void InputCapture_PWMI_Init(void)
{
	//启用GPIOA6
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM3使能
	TIM_InternalClockConfig(TIM3); //选择内部时钟作为时钟源，可省略
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 65536-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
	
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
	TIM_ICInitStructure.TIM_ICFilter = 0xf; //滤波器
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //不分频
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //将TIM3_CH1->TI1->TI1FP1 映射到 IC1
	TIM_PWMIConfig(TIM3,&TIM_ICInitStructure); //此函数代替以下注释内容，自动将另一个通道初始化为相反配置
//	TIM_ICInit(TIM3,&TIM_ICInitStructure);
//	TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
//	TIM_ICInitStructure.TIM_ICFilter = 0xf; //滤波器
//	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //上升沿触发
//	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //不分频
//	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI; //直连通道
//	TIM_ICInit(TIM3,&TIM_ICInitStructure);
	
	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1); //TIM_TS_TI1FP1的上升沿触发从模式，将CNT清零
	TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
	
	TIM_Cmd(TIM3,ENABLE); //启动计时器
}

u32 InputCapture_GetFreq(void)
{
	return 1000000 / (TIM_GetCapture1(TIM3)+1); //实测GetCapture的值会比实际少1，因此+1
}

u32 InputCapture_GetDuty(void)
{
	return (TIM_GetCapture2(TIM3)+1)*100 / (TIM_GetCapture1(TIM3)+1);
}
